package com.zzptc.lhl.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zzptc.lhl.myapplication.entity.RecipeModel;
import com.zzptc.lhl.myapplication.entity.ResultsBean;
import com.zzptc.lhl.myapplication.service.MyAppService;

import org.litepal.LitePal;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("http://liyuankun.cn/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    ListView listView;
    //"http://liyuankun.cn/api/v1/recipe/?limit=10&offset=10"
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        geRecipeInfo();
        getRecipeInfoFromDB();

    }

    private void getRecipeInfoFromDB() {

    }

    private void geRecipeInfo() {
        MyAppService myAppService=retrofit.create(MyAppService.class);
        Call<RecipeModel>caiPuCall=myAppService.getAllRecipe(5);
        caiPuCall.enqueue(new Callback<RecipeModel>() {
            @Override
            public void onResponse(Call<RecipeModel> call, Response<RecipeModel> response) {
                RecipeModel recipeModel=response.body();
                Log.d(TAG, "onResponse: "+recipeModel.getResults().size());
                List<ResultsBean> results = recipeModel.getResults();
                for (ResultsBean r:results){
                    r.save();
                    Log.d(TAG, "onResponse: "+r.save());
                }

            }










            @Override
            public void onFailure(Call<RecipeModel> call, Throwable t) {

            }
        });
    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v=View.inflate(MainActivity.this,R.layout.recipemodel_itm,null);
            ImageView iv_pic=v.findViewById(R.id.iv_pic);
            TextView tv_titel=v.findViewById(R.id.tv_title);


            Glide.with(MainActivity.this)
                    .load(convertView)
                    .into(iv_pic);

            return v;
        }
    }
}
